$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        depTable:null,
        dataGrid:null,
        currentDepId:null,
        currentDepName:null,
        checkId:null,
        form:null,
        stateMenuBar:null,
        titleRow:null,
        tabRow:null,
        tabFrame:null,
        sourceId:null,
        init:function () {
            this.sourceId = xhTool.getQuery('sourceId');
            this.titleRow = $('#titleRow');
            this.tabRow   = $('#tabRow');
            this.depTable = $('#depList');
            this.tabFrame = window.frames['tabFrame'];
            this.dataGrid = new xhDataGrid($('#staffList'),'hrm/resource/getResource',{sourceId:this.sourceId});
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del','bt_list_move','bt_list_group'], ['bt_list_see','bt_list_seeQR','bt_list_interview','bt_list_interviewFlow','bt_list_synchronous']);
            this.listener();
            this.funObj().bindDgEvent();
            this.funObj().initMenu();
            this.funObj().loadDepTree();
        },
        listener:function () {
            var $this = this;
            //查看操作历史
            $('#bt_list_see').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().seeOperate();
            });
            //新增
            $('#bt_list_add').click(function () {
                $this.funObj().openAddTab();
            });

            //删除
            $('#bt_list_del').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().askDelAction();
            });

            //添加面试信息
            $('#bt_list_interview').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
               $this.funObj().openInterview();
            });
            //设置面试负责人
            $('#bt_list_interviewFlow').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().openInterviewerFlow();
            });
            //同步
            $('#bt_list_synchronous').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().doSynchronous();
            });

            $("#bt_list_seeQR").click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().getQR();
            });

            $('#bt_list_search').click(function () {
                if(xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().doSearch();
            });

            xhTool.bindDocKeyDown($('#bt_list_search'));

        },
        funObj:function () {
            var $this = this;
            return {
                loadDepTree:function () {
                    $this.depTable.treegrid({
                        selectOnCheck:false,
                        checkOnSelect:false,
                        onSelect:function (row) {
                            $this.currentDepId=row['iDepId'];
                            $this.currentDepName=row['sDepName'];
                            $this.depTable.treegrid('expand',$this.currentDepId);
                            $('#bt_list_add').setButtonEnable('eu');
                            $('#bt_list_import').setButtonEnable('eu');
                            $this.dataGrid.reload($this.funObj().getParams());
                            $this.dataGrid.command('getPanel').panel('setTitle', '职工列表（' + row['sDepName'] + '）');
                        },
                        onBeforeCheck:function (row) {
                            if(!$this.checkId){
                                $this.checkId = row['iDepId'];
                                return true;
                            }else {
                                if($this.checkId==row['iDepId']){
                                    $this.depTable.treegrid('uncheckRow',row['iDepId']);
                                    return false;
                                }else {
                                    $this.depTable.treegrid('uncheckRow',$this.checkId);
                                }
                            }
                        },
                        onCheck:function (row) {
                            $this.checkId = row['iDepId'];
                        },
                        onLoadSuccess:function (row, data) {
                            $this.depTable.treegrid('select', 0);
                        }
                    })
                },
                getParams:function () {
                    var key=xhExtra.getValue($('#list_search'));
                    var all=xhExtra.getValue($('#select_all'));
                    var workstate=xhExtra.getValue($('#select_work'));
                    var staffstate=xhExtra.getValue($('#select_staff'));
                    var offerstate=xhExtra.getValue($('#select_offer'));
                    var sendInvite=xhExtra.getValue($('#select_sendInvite'));
                    var sendDisqualify=xhExtra.getValue($('#select_sendDisqualify'));
                    return {key:key,all:all,workstate:workstate,staffstate:staffstate,offerstate:offerstate,depId:$this.currentDepId,
                        sendInvite:sendInvite,sendDisqualify:sendDisqualify};
                },
                seeOperate:function () {
                    var id = $this.dataGrid.getSelectValue('iStaffId');
                    if(!id){
                        window.top.xhTop.showError('错误','请选择一个查看的人');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title:'查看',
                        content: '<iframe src="'+moduleUrl+'staff/stateTable/'+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                        size:'lg',
                        backClose: false
                    });
                },
                initMenu:function () {
                    if ($('#bt_list_state').length > 0) {
                        $this.stateMenuBar = $('#bt_list_state').menubutton({
                            iconCls: 'fa fa-user',
                            menu: '#mm'
                        });
                        $($this.stateMenuBar.menubutton('options').menu).menu({
                            onClick: function (item) {
                                //item 的相关属性参见API中的menu
                                var status=0;
                                if(item.text=='未录取'){
                                    status=1;
                                }else if(item.text=='待入职'){
                                    status=2;
                                }else if(item.text=='未入职'){
                                    status=3;
                                }else if(item.text=='入职'){
                                    status=4;
                                }
                                var id = $this.dataGrid.getSelectValue('iStaffId');
                                if(!id){
                                    window.top.xhTop.showError('错误','请选择一个人');
                                    return false;
                                }
                                window.top.xhTop.showModal({
                                    title:item.text,
                                    content: '<iframe src="'+baseUrl+'hrm/staff/stateForm/'+status+'/'+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 270px"  scrolling="yes"   ></iframe>',
                                    button:'提交',
                                    backClose: false
                                });

                            }
                        });
                    }

                    if ($('#bt_menu_sendSms').length > 0) {
                        var sendMenu = $('#bt_menu_sendSms').menubutton({
                            iconCls: 'fa fa-user',
                            menu: '#sendmm'
                        });
                        $(sendMenu.menubutton('options').menu).menu({
                            onClick: function (item) {
                                //item 的相关属性参见API中的menu
                                var ids = $this.dataGrid.getSelectValues('iStaffId');
                                var names = $this.dataGrid.getSelectValues('sStaffName');
                                var info = '确定要对:'+names.join(',')+'这些人发送'+item.text+'么?';
                                var type = '';
                                if(item.text=='邀约短信'){
                                    type = 'invite';
                                }else if(item.text=='未录用短信'){
                                    type = 'unUse';
                                }else {
                                    window.top.xhTop.showError('错误', '未知短信类型');
                                }
                                xhExtra.askChangeStatus('hrm/resource/sendStaffSms', {type:type,ids:ids},null, info, function (res) {
                                    window.top.xhTop.showSuccess(res.msg);
                                    $this.dataGrid.reload();
                                });
                            }
                        });

                    }
                },
                bindDgEvent:function () {
                    $this.dataGrid.onLoadSuccess = function () {
                        $this.dataGrid.clear();
                        //鼠标点击
                        $('.icon-prop').click(function () {
                            $this.funObj().propSign($(this));
                        })
                    };
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        $('#title').contents()[0]['data'] = $(rowData['sStaffName']).text();
                        window.frames['tabFrame'].location.href = baseUrl+'hrm/resource/sourceTab?sourceId='+rowData['iStaffId'];
                        $this.titleRow.show();
                        $this.tabRow.show();
                        xhExtra.forwardToForm(true);
                    };
                    $this.dataGrid.loadFilter = function (data) {
                        for (var i = 0; i < data.rows.length; i++){
                            // var dom =
                            // if (data.rows[i].mSignature) {
                            //     dom = $(dom).attr('nodeBase',data.rows[i].mSignature);
                            //     dom = ''+dom[0]+'';
                            //     console.log(dom);
                            // }
                            data.rows[i].sStaffName = '<a class="icon-prop"  nodeBase="'+data.rows[i].mSignature+'" >'+data.rows[i].sStaffName+'</a>';
                        }
                        return data;
                    };
                },
                goToList:function () {
                    $this.titleRow.hide();
                    $this.tabRow.hide();
                    $this.dataGrid.reload();
                    xhExtra.backToList();
                },
                openAddTab:function () {
                    $('#title').contents()[0]['data'] = '登记人才信息';
                    window.frames['tabFrame'].location.href = baseUrl+'hrm/resource/sourceTab';
                    $this.titleRow.show();
                    $this.tabRow.show();
                    xhExtra.forwardToForm(true);
                },
                askDelAction:function () {
                    var ids = $this.dataGrid.getSelectValues('iStaffId');
                    xhExtra.askChangeStatus('hrm/resource/delSource',{ids:ids},$this.dataGrid,'确认要删除么？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                openInterview:function () {
                    var sourceId = $this.dataGrid.getSelectValue('iStaffId');
                    window.top.xhTop.showModal({
                        title:'面试信息',
                        content: '<iframe src="'+baseUrl+'hrm/resource/interviewTab?sourceId='+sourceId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 400px"  scrolling="yes" ></iframe>',
                        width:800,
                        backClose: false
                    });
                },
                propSign:function (target) {
                    var base64Img = target.attr('nodeBase');
                    if (base64Img) {
                        target.webuiPopover({title:'个人签名',content:'<image src="'+base64Img+'"></image>',closable:true,html:true});
                    }
                },
                openInterviewerFlow:function () {
                    var id = $this.dataGrid.getSelectValue('iStaffId');
                    window.top.xhTop.showModal({
                        title:'设置面试负责人',
                        content: '<iframe src="'+baseUrl+'hrm/resource/interviewerFlow?sourceId='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 420px"  scrolling="yes"   ></iframe>',
                        size:'md',
                        backClose: false,
                        button:'提交'
                    });
                },
                doSynchronous:function () {
                    var id = $this.dataGrid.getSelectValue('iStaffId');
                    xhExtra.askChangeStatus('hrm/resource/synchronousInfo',{sourceId:id},$this.dataGrid,'确认要同步么？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                getQR:function () {
                    var sourceId = $this.dataGrid.getSelectValue('iStaffId');
                    window.top.xhTop.showModal({
                        title:'面试码',
                        content: '<iframe src="'+baseUrl+'hrm/resource/interviewQR?sourceId='+sourceId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 400px"  scrolling="yes" ></iframe>',
                        width:400,
                        backClose: false
                    });
                },
                doSearch:function () {
                    $this.dataGrid.reload($this.funObj().getParams());
                }
            }
        }
    }
};